<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php include('menu.php'); ?>
<script type="text/javascript">
function BrowseCheck(){
	document.formsearch.barcode_search.focus();
}
</script>
</head>
<body onload="BrowseCheck();">
<?php
include("../connect/connect.php");
include('../function/thaidate.php');

	$Queryrr = mysql_query("SELECT member_id FROM member WHERE memtype_id = '1' ");
	$rrnum = mysql_num_rows($Queryrr);
	
//ลบข้อมูล
if($_GET["action"]==del) {

	$Query = "DELETE FROM member WHERE member_id = '".$_GET["id"]."'";
	$objQuery = mysql_query($Query);
	if(!$objQuery)
	{
		echo "Error Delete [".mysql_error()."]";
	} else {
		header('Location: member_manage.php');
	}
}
//ค้นหาพนักงาน
if($_POST["barcode_search"] != "" OR $_POST["titlename_search"] != "" OR $_POST["firstname_search"] != "" OR $_POST["lastname_search"] != "" OR $_POST["idcard_search"] != "" OR $_POST["birthday_search"] != "" OR $_POST["phone_search"] != "") {
	
		$Query = "SELECT * FROM member WHERE memtype_id = '1' ";
	
	if($_POST["barcode_search"] != "") {
		$Query .= "AND member_code LIKE '%".$_POST["barcode_search"]."%' ";
	}
	if($_POST["titlename_search"] != "") {
		$Query .= "AND member_title_name LIKE '%".$_POST["titlename_search"]."%' ";
	}
	if($_POST["firstname_search"] != "") {
		$Query .= "AND member_name LIKE '%".$_POST["firstname_search"]."%' ";
	}
	if($_POST["lastname_search"] != "") {
		$Query .= "AND member_surname LIKE '%".$_POST["lastname_search"]."%' ";
	}
	if($_POST["idcard_search"] != "") {
		$Query .= "AND member_idcard LIKE '%".$_POST["idcard_search"]."%' ";
	}
	if($_POST["birthday_search"] != "") {
		$Query .= "AND member_birthday LIKE '%".$_POST["birthday_search"]."%' ";
	}
	if($_POST["phone_search"] != "") {
		$Query .= "AND member_tel LIKE '%".$_POST["phone_search"]."%' ";
	}
		$Query .= "ORDER BY member_name";
} else {
	$Query = "SELECT * FROM member WHERE memtype_id = '1' ORDER BY member_name";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");

?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการข้อมูลสมาชิก (สมาชิกทั้งหมด <?php echo $rrnum; ?> คน)</h1>
      <div class="buttons"><a onclick="location = 'member_add.php';" class="button"><span>เพิ่มสมาชิก</span></a></div>
    </div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
          	<td class="center">รหัสบาร์โค้ดบัตรสมาชิก</td>
          	<td class="center">คำนำหน้า</td>
            <td class="center">ชื่อ</td>
            <td class="center">นามสกุล</td>
            <td class="center">รหัสบัตรประชาชน</td>
            <td class="center">วันเกิด</td>
            <td class="center">เบอร์โทรศัพท์</td>
            <td class="center">วันที่หมดอายุ</td>
            <td class="center">จัดการข้อมูล</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php echo $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
          	<td align="center"><input name="barcode_search" type="text" id="barcode_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
          	<td align="center"><input size="10" name="titlename_search" type="text" id="titlename_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input name="firstname_search" type="text" id="firstname_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input name="lastname_search" type="text" id="lastname_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input size="13" name="idcard_search" type="text" id="idcard_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input name="birthday_search" type="text" id="birthday_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><input size="10" name="phone_search" type="text" id="phone_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center">&nbsp;</td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		</form>
<?php
	while($row = mysql_fetch_array($objQuery))
		{
?>
          <tr>
          	<td class="left" style="padding-left:20px"><?php echo $row["member_code"];?></td>
          	<td class="left" style="padding-left:20px"><?php echo $row["member_title_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["member_name"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["member_surname"];?></td>
            <td class="center"><?php echo $row["member_idcard"];?></td>
            <td class="center"><?php echo $row["member_birthday"];?></td>
            <td class="center"><?php echo $row["member_tel"];?></td>
            <td class="center"><?php echo thai_date2(strtotime($row["member_date_end"]));?></td>
            <td class="center">[ <a href="member_edit.php?id=<?php echo $row["member_id"];?>">แก้ไข</a> ] [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["member_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
